<?php

/*
Aurus CMS - free OOP CMS
Copyright (C) 2010  Nestor Yanchuk

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('ROOT'))
die();

class Module_static_pages implements Module {
	public function run() {
		//debug($_GET);
		if (isset($_GET['page'])) {
			$this->showPage( (int)$_GET['page'] );
		} else {
			$this->showPage( Config::get('default_page', 'static_pages') );
		}
	}

	protected function showPage($id){
		$page = Mysql::getInstance()->select('pages','*','id='.$id);
		$page = $page[0];

		Aurus::getInstance()->addContent( Parser::getInstance()->parse( stripslashes($page['content'])) );
		return $page;
	}

	public function onActivate() {

	}

	public function onDeactivate() {

	}

	public function initAdminArea() {
		if (isset($_GET['mod']) && $_GET['mod'] == 'add') {
			$this->form(true);
		} elseif (isset($_GET['delete'])) {
			$info = Mysql::getInstance()->delete('pages','id='. (int)$_GET['id']);
		} elseif (isset($_GET['mod']) && $_GET['mod'] == 'edit') {
			$this->getPageList();
		} elseif (isset($_GET['edit_item'])) {
			$this->form(false);
		} else {
			$cfg = new makeConfig('static_pages');
			$cfg
			->select('default_page',__('Сторінка по замовчуванню'),$this->getPageList(true),__('Сторінка, яка буде відображатись, якщо не вказано яку сторіку показати'))
			->get();
		}
	}

	protected function form($new = false){
		if(isset($_POST['name'])){
			$errors = array();

			if (strlen(trim($_POST['name'] )) == 0) {
				$errors[] = __('Заповніть ім\'я сторінки');
			}
			if (strlen(trim($_POST['content'] )) == 0) {
				$errors[] = __('Заповніть вміст сторінки');
			}

			if (!sizeof($errors)) {
				$array = array(
				'parent'	=>	$_POST['parent'],
				'name'		=>	Mysql::getInstance()->escape($_POST['name']),
				'content'	=>	Mysql::getInstance()->escape($_POST['content']),
				);
				if ($new) {
					Mysql::getInstance()->insert('pages',$array);
				} else {
					Mysql::getInstance()->update('pages',$array, 'id='. (int)$_GET['id'] );
				}
				Aurus::getInstance()->addContent(Adminarea::getInstance()->adminInfo(__('Збережено'),0));
			} else {
				Aurus::getInstance()->addContent(Adminarea::getInstance()->adminInfo(implode('<br />',$errors),0));
			}
		} else {
			if ($new) {
				$name = '';
				$parent = '';
				$content = '';
			} else {
				$info = Mysql::getInstance()->select('pages','*','id='. (int)$_GET['id']);
				$info = $info[0];
				debug($info);

				$name = stripslashes($info['name']);
				$parent = stripslashes($info['parent']);
				$content = stripslashes($info['content']);
			}

			$c = Aurus::getInstance()->form_start();

			$select = '<select name="parent">';
			$select .= '<option value="0"> ---------- </option>';
			foreach ($this->getPageList(1) as $id => $parentName) {

				$selected = ((int)$parent == (int)$id) ? ' selected="selected"' : '';
				$select .= '<option'.$selected.' value="'.$id.'">'.$parentName.'</option>';
			}
			$select .='</select>';

			$c .= Html::table(
			Html::tr(
			Html::td('Назва','','left','20%').Html::td('<input type="text" value="'.$name.'" name="name"/>','','left','80%')
			)
			.Html::tr(
			Html::td('Батьківська сторінка','','left','20%').Html::td($select,'','left','80%')
			)
			.Html::tr(
			Html::td('Вміст','','left','20%').Html::td(Aurus::getInstance()->getEditor() . '<textarea id="bb" name="content" style="width: 100%;height:300px">'.$content.'</textarea>','','left','80%')
			)
			.Html::tr(
			Html::td('<center><input type="submit" value="'.__('Зберегти').'"/></center>','','center','100%','top','2')
			)
			) .'</form>';

			Aurus::getInstance()->addContent($c);
		}
	}

	protected function getPageList($asArray = false){
		$list = $this->formatPagesList();
		if ($asArray) {
			return $list;
		}

		$tr = Html::tr(
		Html::td('Назва сторінки','chead','left','70%').
		Html::td('Дія','chead','right','70%')
		);

		foreach ($list as $id => $item) {
			$edit = '<a href="' . LINK_ROOT . 'adminarea/?do=static_pages&amp;edit_item&amp;id='.$id.'">'.__('Змінити').'</a>';
			$delete = '<a onclick="if (!confirm(\'' . __('Дійсно видалити?') . '\')) return false;" href="' . LINK_ROOT . 'adminarea/?do=static_pages&amp;delete='.$id.'">'.__('Видалити').'</a>';

			$tr .= Html::tr(
			Html::td($item,'confclass','left','70%').
			Html::td($edit . ' | ' . $delete,'confclass','right','70%')
			);
		}

		$table = Html::table($tr);

		Aurus::getInstance()->addContent($table);
	}

	function formatPagesList($p = 0, $sub = 0){
		$category = Mysql::getInstance()->select('pages','id,parent,name','','','id ASC');
		$ret = array();

		if (sizeof($category)) {

			foreach ($category as $cat) {
				$id = $cat['id'];
				$name = $cat['name'];
				$parent = $cat['parent'];

				if ($parent == $p) {
					$ret[$id] = str_repeat('-', $sub) . ($sub > 0 ? ' ' : '') . $name;
					$ret += $this->formatPagesList($id, $sub + 1);
				}
			}

		}
		return $ret;
	}

	public function runAjax() {

	}

}